﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "生产派单";
    Layout = null;
}
<link href="~/layui/css/layui.css" rel="stylesheet" />
<script src="~/layui/layui.js"></script>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">任务名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="taskName" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">生产工单号</label>
                    <div class="layui-input-inline">
                        <input type="text" id="orderNumber" placeholder="请输入生产工单号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">工单名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="workstationName" placeholder="请输入工单名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">工序名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="gradingMatch" placeholder="请输入工序名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="searchBtn" data-type="reload">
                        <i class="layui-icon">&#xe615;</i> 搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" id="resetBtn">
                        <i class="layui-icon">&#xe669;</i> 重置
                    </button>
                </div>
            </div>
        </div>
        
        <table id="productionDispatchTable" lay-filter="productionDispatchTable"></table>
    </div>
</div>

<!-- 操作列模板 -->
<script type="text/html" id="operationTpl">
    <a class="layui-btn layui-btn-xs" lay-event="edit">
        <i class="layui-icon">&#xe642;</i> 修改
    </a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">
        <i class="layui-icon">&#xe640;</i> 删除
    </a>
</script>

<script>
    layui.use(['table', 'form', 'layer', 'jquery'], function(){
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;
            
        // 表格实例
        table.render({
            elem: '#productionDispatchTable',
            url: '/ProductionDispatch/ProductionDispatchList',
            method: 'get',
            page: true,
            request: {
                pageName: 'pageIndex',
                limitName: 'pageSize'
            },
            response: {
                statusCode: 0
            },
            parseData: function(res) {
                return {
                    "code": res.code || 0,
                    "msg": res.msg || '',
                    "count": res.count || 0,
                    "data": res.data || []
                };
            },
            cols: [[ // 表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'orderNumber', title: '生产工单编号', width: 120},
                {field: 'taskName', title: '任务名称', width: 120},
                {field: 'workstationName', title: '工作站名称', width: 100},
                {field: 'gradingMatch', title: '工序名称', width: 120},
                {field: 'productName', title: '产品名称', width: 120},
                {field: 'startTime', title: '开始生产时间', width: 160},
                {field: 'endTime', title: '完成生产时间', width: 160},
                {field: 'assignee', title: '派单对象', width: 100},
                {title: '操作', width: 220, toolbar: '#operationTpl', fixed: 'right'}
            ]],
            limits: [10, 20, 30, 50],
            limit: 10
        });
        
        // 搜索按钮点击事件
        $('#searchBtn').on('click', function(){
            var taskName = $('#taskName').val();
            var orderNumber = $('#orderNumber').val();
            var workstationName = $('#workstationName').val();
            var gradingMatch = $('#gradingMatch').val();
            
            table.reload('productionDispatchTable', {
                page: {
                    curr: 1
                },
                where: {
                    taskName: taskName,
                    orderNumber: orderNumber,
                    workstationName: workstationName,
                    gradingMatch: gradingMatch
                }
            });
        });
        
        // 重置按钮点击事件
        $('#resetBtn').on('click', function(){
            $('#taskName').val('');
            $('#orderNumber').val('');
            $('#workstationName').val('');
            $('#gradingMatch').val('');
            
            table.reload('productionDispatchTable', {
                page: {
                    curr: 1
                },
                where: {}
            });
        });
        
       
        
        // 监听工具条事件
        table.on('tool(productionDispatchTable)', function(obj){
            var data = obj.data;
            var event = obj.event;
            
            if(event === 'edit'){
                // 修改派单人信息
                layer.prompt({
                    formType: 0,
                    value: data.assignee || '',
                    title: '请输入派单人名称',
                }, function(value, index){
                    $.ajax({
                        url: '/ProductionDispatch/UpdateProductionDispatch',
                        type: 'post',
                        data: JSON.stringify({
                            id: data.id,
                            assignee: value
                        }),
                        contentType: 'application/json',
                        success: function(res){
                            layer.msg('修改成功', {icon: 1});
                            // 刷新整个表格数据
                            table.reload('productionDispatchTable');
                        }
                    });
                    layer.close(index);
                });
            } else if(event === 'reset'){
                // 重置派单人
                layer.confirm('确定要重置该派单人吗？', function(index){
                    $.ajax({
                        url: '/api/app/production-dispatch/reset-assignee',
                        type: 'post',
                        data: JSON.stringify(data.id),
                        contentType: 'application/json',
                        success: function(res){
                            if(res.code === 0){
                                layer.msg('重置成功', {icon: 1});
                                obj.update({
                                    assignee: ''
                                });
                            } else {
                                layer.msg(res.msg || '重置失败', {icon: 2});
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if(event === 'delete'){
                // 删除派单
                layer.confirm('确定要删除该记录吗？', function(index){
                    $.ajax({
                        url: '/ProductionDispatch/DeleteProductionDispatch?id=' + data.id,
                        type: 'post',
                        success: function(res){
                            layer.msg('删除成功', {icon: 1});
                            // 刷新整个表格数据
                            table.reload('productionDispatchTable');
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

